package com.flh.crm.workbench.web.controller;

import com.flh.crm.settings.domain.User;
import com.flh.crm.settings.service.UserService;
import com.flh.crm.settings.service.impl.UserServiceImpl;
import com.flh.crm.utils.DateTimeUtil;
import com.flh.crm.utils.PrintJson;
import com.flh.crm.utils.ServiceFactory;
import com.flh.crm.utils.UUIDUtil;
import com.flh.crm.vo.PaginationVo;
import com.flh.crm.workbench.domain.Activity;
import com.flh.crm.workbench.domain.ActivityRemark;
import com.flh.crm.workbench.service.ActivityService;
import com.flh.crm.workbench.service.Impl.ActivityServiceImpl;
import javafx.scene.control.Pagination;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class ActivityController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("进入到市场活动控制器");
        String path = request.getServletPath();


        if ("/workbench/activity/getUserList.do".equals(path)){
            getUserList(request,response);

        }else if ("/workbench/activity/save.do".equals(path)){

            save(request,response);

        }else if ("/workbench/activity/pageList.do".equals(path)){

            pageList(request,response);

        }else if ("/workbench/activity/delete.do".equals(path)){

            delete(request,response);

        }else if ("/workbench/activity/getUserListAndActivity.do".equals(path)){

            getUserListAndActivity(request,response);

        }else if ("/workbench/activity/update.do".equals(path)){

            update(request,response);

        }else if ("/workbench/activity/detail.do".equals(path)){

            detail(request,response);

        }else if ("/workbench/activity/getRemarkListByAid.do".equals(path)){

            getRemarkListByAid(request,response);

        }else if ("/workbench/activity/deleteRemark.do".equals(path)){

            deleteRemark(request,response);

        }else if ("/workbench/activity/saveRemark.do".equals(path)){

            saveRemark(request,response);

        }else if ("/workbench/activity/updateRemark.do".equals(path)){

            updateRemark(request,response);

        }





    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行修改备注操作");

        String noteContent = request.getParameter("noteContent");
        String id = request.getParameter("id");
        String editTime = DateTimeUtil.getSysTime();
        String editBy = ((User)request.getSession().getAttribute("user")).getName();
        String editFlag = "1";

        ActivityRemark ar = new ActivityRemark();

        ar.setId(id);
        ar.setNoteContent(noteContent);
        ar.setEditBy(editBy);
        ar.setEditTime(editTime);
        ar.setEditFlag(editFlag);

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.updateRemark(ar);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("success",flag);
        map.put("ar",ar);

        PrintJson.printJsonObj(response,map);




    }

    private void saveRemark(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行添加备注操作");

        String noteContent = request.getParameter("noteContent");
        String activityId = request.getParameter("activityId");
        String id = UUIDUtil.getUUID();
        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User)request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        ActivityRemark ar = new ActivityRemark();

        ar.setId(id);
        ar.setActivityId(activityId);
        ar.setCreateBy(createBy);
        ar.setCreateTime(createTime);
        ar.setEditFlag(editFlag);
        ar.setNoteContent(noteContent);

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.saveRemark(ar);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("success",flag);
        map.put("ar",ar);

        PrintJson.printJsonObj(response,map);



    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {


        System.out.println("删除备注操作");
        String id = request.getParameter("id");

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.deleteRemark(id);

        PrintJson.printJsonFlag(response,flag);


    }

    private void getRemarkListByAid(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("根据市场活动id,取得备注信息列表");

        String activityId = request.getParameter("activityId");

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        List<ActivityRemark> arList= as.getRemarkListByAid(activityId);

        PrintJson.printJsonObj(response,arList);






    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入市场活动详情页");

        String id = request.getParameter("id");

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        Activity a = as.detail(id);

        request.setAttribute("a",a);

        request.getRequestDispatcher("/workbench/activity/detail.jsp").forward(request,response);


    }


    private void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行市场活动修改操作");

        String id =request.getParameter("id");//主键
        String owner =request.getParameter("owner") ;//所有者 外键关联tbl_user
        String name =request.getParameter("name")  ;//姓名
        String startDate =request.getParameter("startDate") ;//开始时间
        String endDate =request.getParameter("endDate")  ;//结束时间
        String cost =request.getParameter("cost") ;//成本
        String description = request.getParameter("description") ;//描述
        //时间
        String editTime= DateTimeUtil.getSysTime();
        String editBy =((User)request.getSession().getAttribute("user")).getName();

        Activity activity = new Activity();

        activity.setId(id);
        activity.setOwner(owner);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);
        activity.setCost(cost);
        activity.setDescription(description);
        activity.setEditTime(editTime);
        activity.setEditBy(editBy);
        System.out.println("owner"+owner);
        System.out.println("editBy"+editBy);
        System.out.println(activity.toString());
        ActivityService as= (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.update(activity);

        PrintJson.printJsonFlag(response,flag);



    }

    private void getUserListAndActivity(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入到查询用户信息列表和根据市场活动查询单条记录");

        String id = request.getParameter("id");
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        /*
        *
        * ulist
        * a
        * 用map即可
        *
        *
        * */
        Map<String,Object> map = as.getUserListAndActivity(id);



        //传回前端
        PrintJson.printJsonObj(response,map);



    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行市场活动删除操作");

        String ids[] = request.getParameterValues("id");
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.delete(ids);

        PrintJson.printJsonFlag(response,flag);
    }

    private void pageList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("进入到查询市场活动信息列表的操作(条件查询+分页查询)");
        System.out.println("开始0");

        String name = request.getParameter("name");

        String owner = request.getParameter("owner");

        String startDate = request.getParameter("startDate");

        String endDate = request.getParameter("endDate");

        String pageNoStr = request.getParameter("pageNo");

        int pageNo =Integer.valueOf(pageNoStr);

        //每页记录数
        String pageSizeStr = request.getParameter("pageSize");
        int pageSize =Integer.valueOf(pageSizeStr);

        //计算出略过的记录数
        int skipCount = (pageNo-1)*pageSize;
        System.out.println("开始1");
        //放到map中传输
        Map<String,Object> map = new HashMap<String, Object>();

        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("skipCount",skipCount);
        map.put("pageSize",pageSize);

        System.out.println("name"+map.get("name"));
        System.out.println("owner"+map.get("owner"));
        System.out.println("startDate"+map.get("startDate"));
        System.out.println("endDate"+map.get("endDate"));
        System.out.println("skipCount"+map.get("skipCount"));
        System.out.println("pageSize"+map.get("pageSize"));

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        /*
        *           前端需要 1.市场活动信息列表
        *                   2.总数
        *
        *           业务层拿到参数返回
        *         map
        *           map.put("pageList",pageList);
        *           map.put("total",total);
        *         然后转成json
        *         vo
        *           PaginationVo<T>
                        private int total;
                        private List<T> dataList;
                   将来分页查询，每个模块都有，写一个通用vo
                   PaginationVo<Activity> vo = new PaginationVo();
                   vo.setTotal(total);
                   vo.setDataList(List)
                   然后转成json


        *
        *
        * */

        PaginationVo<Activity> vo =  as.pageList(map);

        System.out.println("vo"+vo.toString());


        //转成json并发送到前端
        PrintJson.printJsonObj(response,vo);

        //解决数据未显示问题！！！！！！！！！！！

    }

    private void save(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行活动添加操作");
         String id = UUIDUtil.getUUID();//主键
         String owner =request.getParameter("owner") ;//所有者 外键关联tbl_user
         String name =request.getParameter("name")  ;//姓名
         String startDate =request.getParameter("startDate") ;//开始时间
         String endDate =request.getParameter("endDate")  ;//结束时间
         String cost =request.getParameter("cost") ;//成本
         String description = request.getParameter("description") ;//描述
           //时间
        String createTime= DateTimeUtil.getSysTime();
        String createBy =((User)request.getSession().getAttribute("user")).getName();

        Activity activity = new Activity();

        activity.setId(id);
        activity.setOwner(owner);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);
        activity.setCost(cost);
        activity.setDescription(description);
        activity.setCreateTime(createTime);
        activity.setCreateBy(createBy);
        System.out.println("owner"+owner);
        System.out.println(activity.toString());
        ActivityService as= (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.save(activity);

        PrintJson.printJsonFlag(response,flag);



    }

    private void getUserList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("取得用户信息表");

        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> uList = us.getUserList();

        PrintJson.printJsonObj(response,uList);


    }


}












